<?php $this->load->view('sys_header');?>
<script type="text/javascript">
lin.ns('lin.admin.product');
    var infoclass = [
        <?php
        //生成分类js对象
        $j=0;?>
        <?php foreach($info_class as $i):?>
        <?php $j++;?>
        {classid:'<?php echo $i['classid']?>',classname:'<?php echo $i['classname']?>'}<?php if($j!=count($info_class)) { echo ',';}?>

        <?php endforeach; ?>
    ];


    var isgood = [
        {isgood:'0',isgoodname:'不推荐'},
        {isgood:'1',isgoodname:'一级推荐'},
        {isgood:'2',isgoodname:'二级推荐'},
        {isgood:'3',isgoodname:'三级推荐'},
        {isgood:'4',isgoodname:'四级推荐'},
        {isgood:'5',isgoodname:'五级推荐'},
        {isgood:'6',isgoodname:'六级推荐'},
        {isgood:'7',isgoodname:'七级推荐'},
        {isgood:'8',isgoodname:'八级推荐'},
        {isgood:'9',isgoodname:'九级推荐'}
    ];

    $(function(){
        lin.admin.product.searchform = $('#info_searchform').form();
        lin.admin.product.editrow = undefined;
        $('#info_list').datagrid({

            url:'<?php echo made_admin_url('product/rows');?>',
            idField:'id',
            fitColumns:true,
            border:false,
            pagination:true,
            pageSize:10,
            pageList:[10,20,30,40,50,60,70,80,90,100],
            nowrap: false,
            striped: true,
            style:"background: #fff",
            loadMsg : '数据载入中......',
            sortName:'id',
            sortOrder:'desc',

            onLoadError : function() {

                alert('数据加载失败!');

            },
            frozenColumns:[[
                {field:'id',checkbox:true}
            ]],
            columns:[[
                {field:'showid',title:'ID',align:'center',width:30},
                {field:'proname',title:'产品名称',width:fixWidth(0.3),height:80},
                {field:'classid',title:'分类',width:100,align:'center',
                    formatter:function(value){
                    for(var i=0; i<infoclass.length; i++){
                        if (infoclass[i].classid == value) return infoclass[i].classname;
                    }
                    return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'classid',
                            textField:'classname',
                            data:infoclass,
                            editable:false,
                            required:true
                        }
                    }
                },
                {field:'isgood',title:'推荐等级',sortable:true,align:'center',width:60,
                    formatter:function(value){
                        for(var i=0; i<isgood.length; i++){
                            if (isgood[i].isgood == value) return isgood[i].isgoodname;
                        }
                        return value;
                    },
                    editor:{
                        type:'combobox',
                        options:{
                            valueField:'isgood',
                            textField:'isgoodname',
                            data:isgood,
                            editable:false,
                            required:true
                        }
                    }
                },
                {field:'hits',title:'点击量',width:50,align:'center',editor:{
                    type:'validatebox',
                    options:{

                    }
                }},
                {field:'addtime',title:'添加时间',sortable:true,width:100},
                {field:'updatetime',title:'修改时间',sortable:true,width:100},
                {field:'order',title:'排序',width:30,sortable:true,align:'center',
                    editor:{
                        type:'validatebox',
                        options:{

                        }
                    }
                }
            ]],

            toolbar:[{
                text:"添加",
                iconCls:'icon-add',
                handler:function(){
                    window.location.href = '<?php echo made_admin_url('product/addproduct');?>';
                    return false; //hack ie6跳转无效
                }
            },'-',{
                text:"修改",
                iconCls:'icon-edit',
                handler:function(){

                    var rows = $('#info_list').datagrid('getSelections');
                    if(rows.length == 1)
                    {

                        //跳转到编辑页面
                        window.location.href = '<?php echo made_admin_url('product/editmore?id=');?>'+rows[0]['id'];
                        return false; //hack ie6跳转无效

                    }else if(rows.length == 0)
                    {
                        lin.msg('没有选择数据！','error');
                        return;
                    }else if(rows.length > 1)
                    {
                        lin.msg('一次只能修改一条数据！','error');
                        return;
                    }
                }
            },'-',{
                text:"删除",
                iconCls:'icon-remove',
                handler:function(){
                    var rows = $('#info_list').datagrid('getSelections');
                    if(rows.length > 0)
                    {
                        $.messager.confirm('请确认','您确定要删除当前所选择的记录吗？',function(r){
                           if(r)
                           {

                               var id_arr = [];
                               for( var i = 0 ; i < rows.length; i++){
                                   id_arr.push(rows[i].id);
                               }

                               $.ajax({
                                   type:'post',
                                   url:'<?php echo made_admin_url('product/del');?>',
                                   data:{id_arr:id_arr.join(',')},
                                   dataType:"json",
                                   success:function(r){
                                       if(r && r.success)
                                       {
                                           lin.msg('删除记录成功！');
                                           $('#info_list').datagrid('load');
                                           $('#info_list').datagrid('unselectAll');    //取消所有选择

                                       }
                                       else
                                       {
                                           lin.msg('删除数据时出现错误，请联系管理员。','error');
                                       }

                                   }

                               });
                           }
                            else
                           {

                           }
                        });
                    }
                    else
                    {
                        lin.msg('请选择要删除的记录！','error');
                    }
                }
            },'-',{
                text:"撤销",
                iconCls:'icon-redo',
                handler:function(){
                    lin.admin.product.editrow = undefined;
                    $('#info_list').datagrid('rejectChanges');  //回滚编辑前状态
                    $('#info_list').datagrid('unselectAll');    //取消所有选择
                    $('#info_list').datagrid('load');    //刷新
                }
            },'-',{
                text:"保存",
                iconCls:'icon-save',
                handler:function(){
                    $('#info_list').datagrid('endEdit',lin.admin.product.editrow);
                }
            },'-'

            ],

        onAfterEdit:function(rowIndex, rowData, changes){
            var inserted =  $('#info_list').datagrid('getChanges','inserted');
            var updated =  $('#info_list').datagrid('getChanges','updated');

            var url = '';
            if(inserted.length > 0)
            {
                url = '<?php echo made_admin_url('product/insert');?>';
            }
            if(updated.length > 0)
            {
                url = '<?php echo made_admin_url('product/edit');?>';
            }

            //提交数据到后台
            $.ajax({
                type:"post",
                dataType: "json",//返回json格式的数据
                url: url,
                data: rowData,
                success: function(r){
                     if(r && r.success)
                     {
                         $('#info_list').datagrid('acceptChanges');
                         $('#info_list').datagrid('load'); //刷新数据
                         lin.msg(r.msg,'info');

                     }
                    else
                     {
                         $('#info_list').datagrid('rejectChanges');
                         lin.msg(r.msg,'error');
                         $('#info_list').datagrid('load');    //刷新
                     }
                    lin.admin.product.editrow = undefined;
                    $('#info_list').datagrid('unselectAll');    //取消所有选择
                }
            });

        },

        onDblClickRow:function(rowIndex, rowData){
             if(lin.admin.product.editrow != undefined)
             {
                 $('#info_list').datagrid('endEdit',lin.admin.product.editrow);
             }
             else
             {
                 $('#info_list').datagrid('beginEdit',rowIndex);
                 lin.admin.product.editrow = rowIndex;
             }
        },
        onSortColumn:function(){
            //排序操作
        }
        //onClickCell:function(rowIndex, field, value){
            //alert(value);
        //}

        });
    });
    function fixWidth(percent)
    {
        return document.body.clientWidth * percent ; //这里你可以自己做调整
    }

    info_search = function(){
        $('#info_list').datagrid('load',lin.serializeObject(lin.admin.product.searchform));
    }


</script>

<div id="showpic"  title="My Window">
</div>

<div class="nav">
    <h2><a href="<?php echo made_admin_url('main/mainfra');?>">TAYCMS后台管理</a> >  <a href="<?php echo made_admin_url('product');?>">产品管理</a> </h2>
</div>


<div class="search">
    <h1></h1>
    <div class="ml"></div>

    <div>

        <form id="info_searchform">
            <table width="500" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="60" align="right" >关键词：</td>
                    <td align="left"  ><input name="keywords" type="text" class="keywords inputborder" /></td>
                    <td width="50" align="right">栏目：</td>
                    <td  >
                        <div class="select_border">
                            <div class="container">
                                <select name="classid" class="select">
                                    <option value="0">请选择分类</option>

                                        <?php foreach($info_class as $i): ?>
                                        <option value="<?php echo $i['classid']?>"   ><?php echo $i['classname']?></option>
                                        <?php endforeach; ?>

                                </select>
                            </div>
                        </div>

                    </td>
                    <td width="100">
                        <input name="search_submit" type="button" value="查询" class="sh_btn" onclick="info_search();" />
                    </td>
                </tr>
            </table>
        </form>

    </div>

</div>


<div class="list_warp">

    <div class="content_list" style=" border: 1px #ccc solid; ">
        <table id="info_list" ></table>
    </div>


</div>





<?php $this->load->view('sys_footer');?>